<?php

/**
 * IndexController
 * 
 * @author
 * @version 
 */

class IndexController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		//проверка сессии на присутствие переменной языка
		$siteNamespace = new Zend_Session_Namespace('site');
		if(!isset($siteNamespace->lang)) {
			//меняем layout на приветсвие
			Zend_Layout::getMvcInstance()->setLayout('welcome');
			//устанавливаем title
			$this->view->headTitle($this->view->translate('Welcome to site'));
		} else {
			Zend_Layout::getMvcInstance()->setLayout('index');
			//устананавливаем метатеги для главной страницы
			$setting = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
			if($setting) {
				$this->view->headMeta()->appendName('description', $setting->Translation[$siteNamespace->lang]->seo_description);
				$this->view->headMeta()->appendName('keywords', $setting->Translation[$siteNamespace->lang]->seo_keywords);
			}
		}
	}
	
	/**
	 *  Смена языка AJAX
	 */
	public function changelangAction() {
		//Отключаем вид и макет
		Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
		$this->_helper->layout->disableLayout();
		
		//Если получен xmlHttpRequest
		
		if($this->getRequest()->isXmlHttpRequest()){
			//получаем язык
			$lang = $this->_request->getParam('lang', 'ru');
			//заносим в сессию
			$siteNamespace = new Zend_Session_Namespace('site');
			$siteNamespace->lang = $lang;
			//посылаем ответ
			$response = $this->getResponse();
			$response->setBody('done')
						->setHeader('content-type', 'text/plain', true);
		} else {
			throw new Zend_Controller_Action_Exception('Page not found', 404);
		}
	}
	
	/**
	 * 
	 * Поиск по сайту
	 */
	public function searchAction() {
		if($this->_request->isPost()) {
			$values = $this->_request->getPost();
			
			if(isset($values['search']) && !empty($values['search'])) {
				$query = strip_tags(trim($values['search']));
				$searchEngine = new Nimblecms_Search(APPLICATION_PATH . '/../data/search-index2');
				$results = $searchEngine->search($query);
				
				$form = new Default_Form_Search();
				$form->setAction($this->view->url(array(),'default_index_search'));
				
				$element = $form->getElement('search');
				$element->setValue($query);
				
				$this->view->form = $form;
				
				$this->view->hits = $results['hits'];
				$this->view->userQuery = $results['userQuery'];
				
				$this->view->headTitle($this->view->translate('RESULTS OF SEARCH'));
			
				//устананавливаем метатеги для главной страницы
				$setting = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
				if($setting) {
					$this->view->headMeta()->appendName('description', $setting->Translation[$this->view->LANG]->seo_description);
					$this->view->headMeta()->appendName('keywords', $setting->Translation[$this->view->LANG]->seo_keywords);
				}
			} else {
				return $this->_redirect($this->view->url(array(),'default_index_index'));
			}
		} else {
			return $this->_redirect($this->view->url(array(),'default_index_index'));
		}
	}
}
